﻿using log4net;
using log4net.Config;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace XYX.Print.Utils
{

    /// <summary>
    /// 日志记录
    /// </summary>
    public class Logger
    {
        /// <summary>
        /// Logger对象
        /// </summary>
        private static ILog logger;
        
        /// <summary>
        /// 构造
        /// </summary>
        static Logger()
        {
            if (logger == null)
            {
                var repository = LogManager.CreateRepository("Repository");
                XmlConfigurator.Configure(repository, new FileInfo("Log4Configs/log4net.config"));       //log4net从log4net.config文件中读取配置信息
                logger = LogManager.GetLogger(repository.Name, "InfoLogger");
            }
        }

        /// <summary>
        /// 调式
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="exception">异常对象</param>
        public static void Debug(string message, Exception exception = null)
        {
            if (exception == null)
            {
                logger.Debug(message);
            }
            else
            {
                logger.Debug(message, exception);
            }
                
        }

        /// <summary>
        /// 记录信息
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="exception">异常对象</param>
        public static void Info(string message, Exception exception = null)
        {
            if (exception == null)
            {
                logger.Info(message);
            }
            else
            {
                logger.Info(message, exception);
            }
             
        }

        /// <summary>
        /// 警告
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="exception">异常对象</param>
        public static void Warn(string message, Exception exception = null)
        {
            if (exception == null)
            {
                logger.Warn(message);
            }
            else
            {
                logger.Warn(message, exception);
            }
        }

        /// <summary>
        /// 错误
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="exception">异常对象</param>
        public static void Error(string message, Exception exception = null)
        {
            if (exception == null)
            {
                logger.Error(message);
            }

            else
            {
                logger.Error(message, exception);
            }
        }
    }
}

